package com.zhiche.lisa.bms.service.base.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.mapper.base.BaseCommonParameMapper;
import com.zhiche.lisa.bms.dao.model.base.BaseCommonParame;
import com.zhiche.lisa.bms.service.base.BaseCommonParameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linbao
 * @since 2019-08-06
 */
@Service
@Slf4j
public class BaseCommonParameServiceImpl extends ServiceImpl<BaseCommonParameMapper, BaseCommonParame> implements BaseCommonParameService {


    /**
     * 根据分组获取对应的参数
     *
     * @param key
     * @return
     */
    @Override
    public List<BaseCommonParame> getBaseCommonParameListByCode(String key) {
        if (StringUtils.isNotBlank(key)) {
            EntityWrapper<BaseCommonParame> ew = getCommonWrapper(key);
            return this.selectList(ew);
        }
        return null;
    }

    /**
     * 获取对应的参数值
     *
     * @param key
     * @return
     */
    @Override
    public List<String> getBaseCommonValueByKey(String key) {
        List<String> valueList = Lists.newArrayList();

        if (StringUtils.isNotBlank(key)) {
            EntityWrapper<BaseCommonParame> ew = getCommonWrapper(key);
            ew.setSqlSelect("distinct param_value");
            List<Object> objectList = this.selectObjs(ew);
            if (CollectionUtils.isNotEmpty(objectList)) {
                objectList.forEach(obj -> {
                    valueList.add(obj.toString());
                });
            }
            return valueList;
        }
        return null;
    }

    private EntityWrapper<BaseCommonParame> getCommonWrapper(String key) {

        EntityWrapper<BaseCommonParame> ew = new EntityWrapper<>();
        ew.eq("param_group", key);
        return ew;
    }
}
